# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Collective victimhood beliefs and conflict-related attitudes: A meta-analysis
# Marko Kljajic, Nadav Shelef, and Ethan vanderWilden

# Last Replicated: August 6, 2025

# Analysis file
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

############ 1. Load in packages, data, helper functions ############
# List of packages
pkg = c("tidyverse", "patchwork", "metafor", "kableExtra", "rmeta", "reshape2", "metaSEM")

# Checks if they are installed, install if not
if (length(setdiff(pkg, rownames(installed.packages()))) > 0) {install.packages(setdiff(pkg, rownames(installed.packages()))) }

# Load + clear environment
suppressMessages(suppressWarnings(lapply(pkg, library, character.only = TRUE)))
rm(list=ls())

# set working directory (NOTE: change for local file path)
setwd("C:/Users/19785/Desktop/CPS_replication")

# load in the dataset
data <- read.csv("KSV_dataset.csv")

#Load in functions from file
source("helperFunctions.R")


### Main analysis excludes 'bundled' treatments ; emotions/cognitive perspective outcomes
main <- subset(data, data$include.main == 1) # (keep emotions and cognitive frames)
main <- subset(main, main$outcome %in% c("Hawkishness", "Ingroup Attachment", "Outgroup Exclusion", "Reconciliation"))



############ 2. Main Results ####

### Table 1: Number of estimates
kbl(get_n(main, exclude_mechs = T)$ntab, format = 'latex', booktabs = T)


### Figure 3: Full individual estimate plot
meta_plot(main, sepColor = T) # 12 x 6 pdf

### Figure 4 (A-D): Individual Estimate Plots
meta_plot(subset(main, main$treatment == "Collective victimhood"), includeEst = T) # 6 x 4
meta_plot(subset(main, main$treatment == "Competitive victimhood"), includeEst = T) # 6 x 4
meta_plot(subset(main, main$treatment == "Exclusive victimhood"), includeEst = T) # 6 x 4
meta_plot(subset(main, main$treatment == "Inclusive victimhood"), includeEst = T) # 6 x 4


### In-text estimates (written/mentioned)
# effect sizes: (see column 1)
results <- fill_metatable3(main, exclude_mechs = T) 
results
# Overall: 0.274(0.044), Generic: 0.333 (0.046), Competitive: 0.495, 0.82,
#   Exclusive: 0.389 (0.096), Inclusive: -0.26 (0.070)

# estimates range from -2.49 to 2.49
round(range(main$d.directional) , 2)

#combined Non-Inclusive victimhood (estimate of 0.391, SE = 0.040)
summary(meta3L(y = subset(main, main$treatment != "Inclusive victimhood")$d.directional, 
               v = subset(main, main$treatment != "Inclusive victimhood")$var.d, 
               cluster = subset(main, main$treatment != "Inclusive victimhood")$article.id))


############ 3. Combinations of CVB and Conflict attitudes ####

### Figure 5: Main coefficient plot
full_plots(results, df = main, exclude_mechs = T, Bonf = T, range = 1.5) # 10 x 10 pdf

#Included in the text: What is the p-value of non-significant results?
round(pnorm(-abs(0.209 /(0.171))), 3) #Competitive-Hawkishness [p = 0.111]
round(pnorm(-abs(0.237 /(0.182))), 3) #Exclusive-Hawkishness [p = 0.096]

round(pnorm(-abs(0.404 /(0.207))), 3) #Generic-Outgroup Exclusion [p = 0.025] ### 025!!
round(pnorm(-abs(0.234 /(0.132))), 3) #Exclusive-Outgroup Exclusion [p = 0.038]

round(pnorm(-abs(-0.139 /(0.094))), 3) #Inclusive-Outgroup Exclusion [p = 0.070]
round(pnorm(-abs(0.108 /(0.139))), 3) #Inclusive-Ingroup Attachment [p = 0.219]



############ 4. Consistency across Contexts ####

### Figure 6: estimates by setting
subset_plots(subset_estimates(main, "Setting"), separate = T, range = 2.5, ind_plots = T) # 8 x 7 render for each (2 plots)

#Included in the text: Proportion of estimates from US or Israel? (37%)
round(nrow(subset(main, main$Setting == "Israel" | main$Setting == "United States"))/745, 2)


### Figure 7: estimates by victimhood dimension
subset_plots(subset_estimates(main, "victimhood.dimension"), range = 1.5, jump = 0.5) #9 x 6


### Figure 8: estimates by conflict type (NOTE: reset factor orders)
conflict <- subset_estimates(main, "conflict.type")
conflict$setting <- factor(conflict$setting,  levels = c("Genocide/\nMass Violence",  "Colonialism/\nSlavery", "Armed\nConflict", "Discrimination"))
subset_plots(conflict, range = 1.5, jump = 0.5)  #9 x 6



############ 5. Consistency across Methods ####
### Figure 9: Experimental and Observational studies

exp <- fill_metatable3(subset(main, main$Design.binary == "Experimental"), exclude_mechs = T) 
exp[1, 1] #REPORTED in paper: overall, d = 0.062, se = 0.03
obs <- fill_metatable3(subset(main, main$Design.binary == "Observational"), exclude_mechs = T) 
obs[1, 1] #REPORTED in paper: overall, d = 0.353, se = 0.056
comparison_plot(exp, obs, subset(main, Design.binary == "Experimental"),  subset(main, Design.binary == "Observational"),
             exclude_mechs = T) #12 x 6


############ 6. Publication Bias ####

### Figure 10: Regression test for publication bias
Edata <- subset(main, main$Design.binary == "Experimental")
Odata <- subset(main, main$Design.binary != "Experimental")
summary(lm(d ~ sd.d, data = main)) # d = 0.367 + 0.846*SE
summary(lm(d ~ sd.d, data = Edata)) # d = -0.026 + 1.3576*SE
summary(lm(d ~ sd.d, data = Odata)) # d = 0.418 + 1.196*SE

ggplot() +
  geom_point(aes(x = Edata$sd.d, y = Edata$d), alpha = 0.4, color = "darkblue") +
  geom_point(aes(x = Odata$sd.d, y = Odata$d), alpha = 0.4, color = "darkred") +
  
  geom_smooth(aes(x = data$sd.d, y = data$d), method = "lm", color = "black", lwd = 1.5, se = F) +
  geom_smooth(aes(x = Edata$sd.d, y = Edata$d), method = "lm", color = "darkblue", lwd = 1.5, se = F) +
  geom_smooth(aes(x = Odata$sd.d, y = Odata$d), method = "lm", color = "red", lwd = 1.5, se = F) +
  
  
  theme_bw() + labs(y = "Effect size magnitude", x = "Estimate SE (converted for effect size)", 
                    subtitle = "Overall (green): d = 0.367 + 0.846*SE\nExperimental (blue): d = -0.026 + 1.358*SE\n
                    Observational (red): d = 0.418 + 1.196*SE") +
  scale_y_continuous(limits = c(0, 2.5)) +
  theme(text = element_text(size = 15), plot.subtitle = element_blank()) # 12 x 6


############ 7. Emotions and Cognitive Perspectives ####

#get dataset with Emotions and Cognitive Perspectives (but still excluding bundled treatments)
main_wECP <- subset(data, data$include.main == 1) # (keep emotions and cognitive frames)

### Figure 11: estimates on cognitive frames and emotions
full_plots(fill_metatable3(main_wECP), df = data, only_mech = T, range = 2) # 10 x 4

